# import time
# import pytest
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# # from selenium.webdriver.support.wait import WebDriverWait
# # from selenium.webdriver.support import expected_conditions as EC
#
# # 封装一个测试登录的类
# class Testlogin:
#
#
#     def test_admin_login(self):
#         # 打开浏览器
#         driver = webdriver.Chrome()
#         time.sleep(2)
#         driver.get("http://localhost:90/")
#         driver.maximize_window()
#
#         # 获取登录页面元素
#         time.sleep(2)
#         driver.find_element(By.XPATH, "//input[@id = 'username']").send_keys("admin")
#         driver.find_element(By.ID, "password").send_keys("123456")
#         driver.find_element(By.ID, "login_btn").click()
#
#         time.sleep(3)
#
#         # 获取实际结果
#         result = driver.find_element(By.XPATH, "//*[@id='logo']/a").text
#
#         # 判断
#         if result == "后台管理系统":
#             print("登陆成功")
#         else:
#             driver.save_screenshot("不通过截图1.png")
#             print("登陆失败")
#
#         assert result == "后台管理系统"

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


class Loginchecker:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def text_entera(self):
        un = self.username
        p = self.password
        try:
            # 创建一个谷歌浏览器对象
            a = webdriver.Chrome()

            # 打开系统网址
            a.get("http://localhost:90")

            # 窗口最大化
            a.maximize_window()

            time.sleep(2)
            WebDriverWait(a, 4, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'lear-login')))

            # 输入用户名，密码
            a.find_element(By.ID, 'username').send_keys(f'{un}')
            a.find_element(By.NAME, 'password').send_keys(f'{p}')

            # 点击登录
            a.find_element(By.XPATH, '//*[@id="login_btn"]').click()

            # 等待显示系统首页
            WebDriverWait(a, 5, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="logo"]/a')))
            r1 = a.find_element(By.XPATH, '//*[@id="logo"]/a').text
            if r1 == '后台管理系统':
                print("登录成功！")
            else:
                print("登录失败！")
        except Exception as e:
            print("登录失败！", e)

    def text_enteru(self):
        un = self.username
        p = self.password
        try:
            # 创建一个谷歌浏览器对象
            a = webdriver.Chrome()

            # 打开系统网址
            a.get("http://localhost:90")

            # 窗口最大化
            a.maximize_window()

            time.sleep(2)
            WebDriverWait(a, 4, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'lear-login')))

            # 输入用户名，密码
            a.find_element(By.ID, 'username').send_keys(f'{un}')
            a.find_element(By.NAME, 'password').send_keys(f'{p}')

            # 点击登录
            a.find_element(By.XPATH, '//*[@id="login_btn"]').click()

            # 等待显示系统首页
            WebDriverWait(a, 5, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="logo"]/a')))
            r1 = a.find_element(By.XPATH, '//*[@id="logo"]/a').text
            if r1 == '个人理财系统':
                print("登录成功！")
            else:
                print("登录失败！")
        except Exception as e:
            print("登录失败！", e)

if __name__ == '__main__':
    l = Loginchecker('admin', '123456')
    l.text_entera()

